package com.xixiake.net;

import com.xixiake.application.BaseApplication;
import com.xixiake.common.AppSetting;
import com.xixiake.common.Config;
import com.xixiake.utils.AppUtil;
import com.xixiake.utils.Constant;
import com.xixiake.utils.DeviceUtil;
import com.xixiake.utils.SharePerferenceUtil;

import java.lang.reflect.Field;
import java.net.URI;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * HTTP请求参数
 * Created by c-wanghengli on 2017/4/16.
 */
public abstract class BaseInput<T> {

    private static final String TAG = BaseInput.class.getName();

    public String url;

    public int method;

    public Map<String,String> cookies;

    public Class<T> clazz;

    protected BaseInput(String url,int method,Class<T> clazz){
        if(AppSetting.isLogDebugVersion){
            this.url = Config.DEBUG_SERVER_ADDRESS + url;
        }else{
            this.url = Config.RELEASE_SERVER_ADDRESS + url;
        }
        this.method = method;
        this.clazz = clazz;
        this.cookies = getCookies();
    }

    /**
     * 获取用户最基本信息
     * @return
     */
    private Map<String,String> getCookies(){
        Map<String,String> cookies = new HashMap<>();
        String userId = String.valueOf(SharePerferenceUtil.getInt(Constant.USERID,-1));
        String accessToken = SharePerferenceUtil.getString(Constant.ACCESSTOKEN,"");
        String client = Constant.CLIENT_TYPE;
        String mid = DeviceUtil.getIMEI();
        if(!"-1".equals(userId)){
            cookies.put(Constant.USERID,userId);
        }
        if(!"".equals(accessToken)){
            cookies.put(Constant.ACCESSTOKEN,accessToken);
        }
        cookies.put(Constant.CLIENT,client);
        if(!"".equals(mid) || "0".equals(mid)){
            cookies.put(Constant.MID, mid);
        }
        cookies.put(Constant.C_VERSON, AppUtil.getVersionCode(BaseApplication.getContext()) + "");
        return cookies;
    }

    public Map<String,Object> getParams(){
        Class<? extends BaseInput> clazz = this.getClass();
        Field[] fields = clazz.getDeclaredFields();//获取所有声明的变量
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("u", DeviceUtil.getIMEI());
        for(Field field : fields){
            //获取注解字段
            InputKey prop = field.getAnnotation(InputKey.class);
            if(prop == null){
                continue;
            }
            field.setAccessible(true);
            Class<?> type = field.getType();
            try {
                if(String.class == type || URL.class == type || URI.class == type) {
                    params.put(prop.name(), field.get(this).toString());
                }else if(double.class == type || Double.class == type){
                    params.put(prop.name(),field.getDouble(this));
                }else if(float.class == type || Float.class == type){
                    params.put(prop.name(),field.getFloat(this));
                }else if(int.class == type || Integer.class == type){
                    params.put(prop.name(),field.getInt(this));
                }else if(long.class == type || Long.class == type){
                    params.put(prop.name(),field.getLong(this));
                }else if(short.class == type || Short.class == type){
                    params.put(prop.name(),field.getShort(this));
                }else if(byte.class == type || Byte.class == type){
                    params.put(prop.name(),field.getByte(this));
                }else if(boolean.class == type || Boolean.class == type){
                    params.put(prop.name(),field.getBoolean(this));
                }else if(Date.class == type){
                    params.put(prop.name(),((Date)field.get(this)).getTime());
                }else if(byte[].class == type){
                    params.put(prop.name(),field.get(this));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return params;
    }

}
